<template>
	<view class="page">
		<view class="search">
			<view class="search-block">
				<view class="search-ico-wapper">
					<image src="../../static/icos/search.png" class="search-ico"></image>
				</view>
				<input placeholder="搜索预告" confirm-type="search" @confirm="searchMe" maxlength="10" class="search-text" focus/>
			</view>
		</view>
		<view class="movie-list page-block">
			<view class="movie-wapper" v-for="(item,index) in list" :key="index" @click="particulars" :data-id="item.id">
				<image class="movie-wapper" :src="item.cover"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				keywords:'',//搜索的内容
				page:1, //当前页数
				totalPages:1 //总页数
			}
		},
		onLoad(){
			this.SearchList('',1,15);
		},
		//页面上拉加载数据
		onReachBottom(){
			this.onRdach();
		},
		//下拉刷新
		onPullDownRefresh(){
			this.keywords= "";
			this.page = 1;
			this.totalPages = 1;
			this.SearchList('',1,15);
		},
		methods: {
			//猜你列表喜欢数据
			//查询数据类容，当前页码，当前页数据总条数
			SearchList(keywords,page,totalPages){ 
				uni.showLoading({mask:true,title:'请稍后...'});//显示 loading 提示框
				uni.showNavigationBarLoading();//在当前页面显示导航条加载动画
				
				uni.request({
					url:this.serverUrl + '/search/list' + this.user_authority+'&keywords='+keywords+'&page='+page+'&pageSize='+totalPages+'',
					method:"POST",
					success: (res) => {
						if(res.data.status == 200){
							let list = res.data.data.rows;
							this.list = this.list.concat(list);//数组连接，返回为一个新的数组
							
							this.totalPages = res.data.data.total;//获取总页数
							this.page = page; //覆盖当前页面的数据
							
							if(this.list.length == 0){
								uni.showToast({
								    title: '抱歉,没有找到你想要的...',
								    duration: 2000,
									icon:'none'
								});
							}
						}
					},
					complete:()=>{
						uni.stopPullDownRefresh();//停止下拉刷新
						uni.hideNavigationBarLoading();//隐藏页面导航条加载动画
						uni.hideLoading();//隐藏 loading 提示框
					}
				})
			},
			//搜索查询
			searchMe(e){
				//获取搜索的内容
				this.keywords = e.detail.value;
				this.list = [];//重置
				this.SearchList(this.keywords,1,15);
			},
			//下拉加载时触发
			onRdach(){
				let page = this.page + 1; //查询下一页面，当前页面数量加1
				let keywords = this.keywords; //获得当前页面中data里的搜索值
				let totalPages = this.totalPages; //获得总页数
				
				//如果当前需要分页的分页数和总页数相等，就不分页
				if(page > totalPages){
					return;//不再往后面执行
				}
				this.SearchList(keywords,page,15);
			},
			//详情
			particulars(e){
				let id = e.currentTarget.dataset.id;
				//页面跳转接口api
				uni.navigateTo({url:'../movie/movie?id='+id});
			}
		}
	}
</script>

<style>
	@import url("search.css");
</style>
